package b1.mobile.blackberry.screen;

import org.json.me.JSONObject;

import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MapsArguments;
import net.rim.blackberry.api.invoke.MessageArguments;
import net.rim.blackberry.api.invoke.PhoneArguments;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import b1.mobile.blackberry.controller.AgentController;
import b1.mobile.blackberry.entity.BusinessPartnerEntity;
import b1.mobile.blackberry.io.http.ResponseParserHelper;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.io.http.Serviceworker.ServiceDelegate;
import b1.mobile.blackberry.io.http.SoapContentProvider;
import b1.mobile.blackberry.ui.component.InfoHeader;
import b1.mobile.blackberry.ui.component.ListGroupItemField;
import b1.mobile.blackberry.ui.component.ListGroupItemManager;
import b1.mobile.blackberry.ui.component.util.FieldFactory;
import b1.mobile.blackberry.ui.component.util.ThemeUtil;
import b1.mobile.blackberry.util.OpenFileUtil;

public class BPInfoScreen extends BaseFunctionScreen implements ServiceDelegate {
	private Bitmap header;
	private String id;
	private BusinessPartnerEntity businessPartner;
	
	public BPInfoScreen(String id) {
		this.id = id;
		
		setTitle(FieldFactory.makeScreenTitle("BP Info"));
		ThemeUtil.decorateScreen(this);
	}
	
	protected void onUiEngineAttached(boolean attached) {
		if (attached){
			onLoadData();
		}
	}

	private void onLoadData() {
		String[] names = new String[]{"CardCode"};
		String[] values = new String[] {this.id};
		SoapContentProvider soap = new SoapContentProvider("GetBPDetail", names, values);
		
		try{
			Serviceworker worker = new Serviceworker(this, AgentController.getServer(), soap);
			(new Thread(worker)).start();
		} catch (Exception ex){
			
		}
	}

	public void onGetResult(Serviceworker worker) {
		if(worker.responseCode == 200){
			ResponseParserHelper help = new ResponseParserHelper(worker.responseMessage);
			JSONObject json = (JSONObject)help.parse("GetBPDetailResult");
			this.businessPartner = new BusinessPartnerEntity(json);
			UiApplication.getUiApplication().invokeLater(new Runnable(){
				public void run() {
					bind();
				}
			});
		} else {
			super.onGetResult(worker);
		}
	}

	private void bind() {		
		String bpType = this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_CARDTYPE);
		if (bpType.equals("Customer")){
			header = Bitmap.getBitmapResource("_partner_detail_customer.png");
//			bpType = "Customer";
		} else if (bpType.equals("Lead")){
			header = Bitmap.getBitmapResource("_partner_detail_lead.png");
//			bpType = "Lead";
		} else if (bpType.equals("Vendor")){
			header = Bitmap.getBitmapResource("_partner_detail_vendor.png");
//			bpType = "Vendor";
		}

		InfoHeader headerField = new InfoHeader(header,
				this.businessPartner
						.getStringProperty(BusinessPartnerEntity.JK_CARDNAME),
				this.businessPartner
						.getStringProperty(BusinessPartnerEntity.JK_CARDCODE), bpType);

		ListGroupItemManager accMgr = new ListGroupItemManager();
		accMgr.setPadding(10, 0, 10, 0);
		ListGroupItemField item = new ListGroupItemField(headerField);
		accMgr.add(item);
		
//		//banner
//		item = new ListGroupItemField("BP Info", Field.NON_FOCUSABLE);
//		item.setType(ListGroupItemField.TYPE_LABEL);
//		accMgr.add(item);
		
		item = new ListGroupItemField(
				resources.getString(LABEL_ACCT_BALANCE),
				this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_ACCOUNTBAL),
				false);
		accMgr.add(item);

		item = new ListGroupItemField(resources.getString(LABEL_CREDIT_LIMIT),
				this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_CREDITLIMT),
				false);
		accMgr.add(item);
		this.add(accMgr);

		// contact
//		ListGroupItemManager contactMgr = new ListGroupItemManager();
		item = new ListGroupItemField(resources.getString(LABEL_TELEPHONE), this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_PHONENUM), true);
		item.setChangeListener(new FieldChangeListener(){
			public void fieldChanged(Field arg0, int arg1) {
				onPhoneCall();
			};
		});
		accMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABEL_EMAIL), this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_EMAIL), true);
		item.setChangeListener(new FieldChangeListener(){
			public void fieldChanged(Field arg0, int arg1){
				onEMail();
			}
		});
		accMgr.add(item);
		
		item = new ListGroupItemField(resources.getString(LABEL_WEB_SITE), this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_WEBSITE), true);
		item.setChangeListener(new FieldChangeListener(){
			public void fieldChanged(Field arg0, int arg1){
				onWebsite();
			}
		});
		accMgr.add(item);
//		this.add(contactMgr);
		
		
		//mainContact
//		ListGroupItemManager mainContactMgr = new ListGroupItemManager();
		item = new ListGroupItemField(resources.getString(LABEL_MAIN_CONTACT), this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_CONTACTPERSON), false);
//		item.setChangeListener(new FieldChangeListener() {
//			public void fieldChanged(Field field, int context) {
//				onMainContact();
//			}
//		});
		
		accMgr.add(item);
		
		item = new ListGroupItemField("Address", this.businessPartner.getAddress(), false);
//		item.setChangeListener(new FieldChangeListener() {
//			public void fieldChanged(Field field, int context) {
//				onAddress();
//			}
//		});
		accMgr.add(item);
	}

	protected void onWebsite() {
		String website = this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_WEBSITE);
		OpenFileUtil.openAddressInBrowser(website);
	}

	protected void onEMail() {
		String to = this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_EMAIL);
		
		MessageArguments email = new MessageArguments(MessageArguments.ARG_NEW, to, "", "Send by BlackBerry");
		Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, email);
	}

	protected void onPhoneCall() {
		String phoneNum = this.businessPartner.getStringProperty(BusinessPartnerEntity.JK_PHONENUM);
		
		PhoneArguments phoneCall = new PhoneArguments(PhoneArguments.ARG_CALL, phoneNum);
        Invoke.invokeApplication(Invoke.APP_TYPE_PHONE, phoneCall);
	}

	protected void onAddress() {
		final String address = this.businessPartner.getAddress();
		Runnable running = new Runnable(){
			public void run(){
				MapsArguments map = new MapsArguments(MapsArguments.ARG_LOCAL_SEARCH, "BP", address);
				Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, map);
			}
		};
		
//		UiApplication.getUiApplication().invokeAndWait(running);
		
		Thread newThread = new Thread(running);
		newThread.start();
		
//		Thread mapThread = new Thread(new Runnable(){
//			public void run(){
//				
//			}
//		});
//		mapThread.setPriority(Thread.MIN_PRIORITY);
//		mapThread.start();
		
	}	
}
